package cn.com.ebidding.business.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class JsonUtils {
    private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);

	public static <T> T toBean(String content, Class<T> valueType) {

		try {
			return JSONObject.parseObject(content, valueType);
		} catch (Exception e) {
            logger.error("",e);
		}
		return null;
	}

	public static <T> T toBean(String content, TypeReference<T> valueTypeRef) {

		try {
			return JSONObject.parseObject(content, valueTypeRef, null);
		} catch (Exception e) {
			logger.error("",e);
		}

		return null;
	}

	public static <T> List<T> toList(String content, Class<T> valueType) {

		try {
			return JSONObject.parseArray(content, valueType);

		} catch (Exception e) {
			logger.error("",e);
		}
		return null;
	}

	public static String toJSon(Object object) {

		try {
			return JSONObject.toJSONString(object);
		} catch (Exception e) {
			logger.error("",e);
		}

		return null;
	}

	public static String toJSonNotNull(Object object) {

		try {
			return JSONObject.toJSONString(object, SerializerFeature.WriteMapNullValue);
		} catch (Exception e) {
			logger.error("",e);
		}

		return null;
	}
}
